package com.example.xymarket.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Toast;

import com.example.xymarket.adapter.MessengerAdapter;
import com.example.xymarket.BuildConfig;
import com.example.xymarket.common.ChatConfig;
import com.example.xymarket.common.Info;
import com.example.xymarket.myView.StatusBar;
import com.example.xymarket.R;
import com.farsunset.cim.sdk.android.CIMEventListener;
import com.farsunset.cim.sdk.android.CIMListenerManager;
import com.farsunset.cim.sdk.android.CIMPushManager;
import com.farsunset.cim.sdk.android.model.Message;
import com.farsunset.cim.sdk.android.model.ReplyBody;
import com.farsunset.cim.sdk.android.model.SentBody;

import java.util.ArrayList;
import java.util.List;


public class MessageActivity extends AppCompatActivity implements CIMEventListener {

    private ProgressDialog progressDialog;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message);
        StatusBar.setStatusBarColor(this,StatusBar.yellow);
        CIMListenerManager.registerMessageListener(this);
        initCIM();
    }

    public void initCIM()
    {
        try {
            progressDialog = new ProgressDialog(this);
            progressDialog.setTitle("提示");
            progressDialog.setMessage("正在连接......");
            progressDialog.show();
            CIMPushManager.connect(this, BuildConfig.CIM_SERVER_HOST, BuildConfig.CIM_SERVER_PORT);
        }
        catch (Exception e)
        {
            System.out.println(e.toString());
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        CIMListenerManager.removeMessageListener(this);
    }
    @Override
    public void onMessageReceived(Message message) {

    }

    @Override
    public void onReplyReceived(ReplyBody replyBody) {
        progressDialog.dismiss();
    }

    @Override
    public void onSendFinished(SentBody sentBody) {

    }

    @Override
    public void onNetworkChanged(NetworkInfo networkInfo) {

    }

    /**
     * 连接cim推送服务端成功回调
     * @param b
     */
    @Override
    public void onConnectFinished(boolean b) {
        /*
         * 第二步 绑定用户id到长连接
         * 这里业务方可以设置token等由后端实现转换成用户id
         */
        CIMPushManager.bind(this,String.valueOf(Info.getMyUserInfo().getId()));
        initRecycleListView();
    }

    @SuppressLint("HandlerLeak")
    public final Handler handler=new Handler()
    {
        @SuppressLint("HandlerLeak")
        @Override
        public void handleMessage(@NonNull android.os.Message msg) {
            super.handleMessage(msg);
            if(msg.what==0)
            {
                Intent intent=new Intent(getApplicationContext(),ChatActivity.class);
                startActivity(intent);
            }
        }
    };

    public void setReceiver()
    {
        for(ChatConfig.chatUser chatUser:ChatConfig.users)
        {
            if(chatUser.id.equals(String.valueOf(Info.getMyUserInfo().getId())))
            {
                ChatConfig.me=chatUser;
            }
            else ChatConfig.him=chatUser;
        }
    }

    public List<ChatConfig.chatUser> chatUserlist= new ArrayList<>();

    public MessengerAdapter messengerAdapter;

    public RecyclerView messageListView;
    private void initRecycleListView() {
        chatUserlist.clear();
        setReceiver();
        chatUserlist.add(ChatConfig.him);
        messageListView = findViewById(R.id.recyclerView);
        messageListView.setLayoutManager(new LinearLayoutManager(this));
        messageListView.setItemAnimator(new DefaultItemAnimator());
        messengerAdapter=new MessengerAdapter(chatUserlist,handler,this);
        messageListView.setAdapter(messengerAdapter);
    }

    @Override
    public void onConnectionClosed() {

    }

    /**
     * 连接cim推送服务端失败回调
     * @param
     */
    @Override
    public void onConnectFailed() {
        progressDialog.dismiss();
        Toast.makeText(this,"连接失败! 请检查Host和Port是否正确，",Toast.LENGTH_LONG).show();

    }

    @Override
    public int getEventDispatchOrder() {
        return 0;
    }


    public void toClassify(View view)
    {
        Intent intent = new Intent(getApplicationContext(), ClassifyActivity.class);
        startActivity(intent);overridePendingTransition(0, 0);
    }

    public void toShoppingCart(View view)
    {
        Intent intent = new Intent(getApplicationContext(), ShoppingCartActivity.class);
        startActivity(intent);overridePendingTransition(0, 0);
    }

    public void toMine(View view)
    {
        Intent intent = new Intent(getApplicationContext(), MineActivity.class);
        startActivity(intent);overridePendingTransition(0, 0);
    }

    public void toHome(View view)
    {
        Intent intent = new Intent(getApplicationContext(), HomeActivity.class);
        startActivity(intent);overridePendingTransition(0, 0);
    }

    public void toChat(View view)
    {
        Intent intent = new Intent(getApplicationContext(), ChatActivity.class);
        startActivity(intent);overridePendingTransition(0, 0);
    }
}